
class UserMgr{
    //金币
    public gold:number;

    //分享次数
    public shareTime: number;

    //章节
    public chapter:number;
    //level
    public level:number;
    //引导
    public hideCaozuo:number;
    //新手礼包是否领取
    public gift1:boolean;
    //领取登录礼包时间
    public giftTime:string;

    public constructor() {
		// egret.localStorage.removeItem("USERINFO");
		let storage = egret.localStorage.getItem("USERINFO");
		if (storage) {
			this.initData(JSON.parse(storage));
		} else {
			this.gold = 200;
            this.chapter = 1;
            this.level = 1;
            this.hideCaozuo = 0;
            this.gift1 = false;
            let nowDate  = new Date();
            let key = nowDate.getFullYear().toString() + nowDate.getMonth().toString() + nowDate.getDate().toString();
            this.giftTime = key;
		}
	}


    public initData(data: any): void {
        this.gold = data.gold;
        this.chapter = data.chapter;
        this.level = data.level;
        this.hideCaozuo = data.hideCaozuo;
        this.gift1 = data.gift1;
        this.giftTime = data.giftTime;
		
	}

    //增加金币
    public addGold(num :number):void{
        this.gold += num;
		if (this.gold < 0) this.gold = 0;
        zysx.NotifyMgr.sendNotification(NotifyConst.UPDATE_GOLD);
		this.saveUserInfo();
    }
    //减少金币
    public decGold(num:number):void{
        this.gold -= num;
		if (this.gold < 0) this.gold = 0;
        zysx.NotifyMgr.sendNotification(NotifyConst.UPDATE_GOLD);
		this.saveUserInfo();
    }
    //是够可以扣除金币
    public canDecGold(num:number):boolean{
        return this.gold >= num;
    }

    //设置关卡等级
    public setlevel(chapter:number,level:number):void{
        this.level = level;
        this.chapter = chapter;
        this.saveUserInfo();
    }

    //领取新手礼包
    public getNewGift(){
        if(!this.gift1){
            this.gift1 = true;
            this.saveUserInfo();
        }
    }

    //领取登录礼包
    public getDayGift(){
        let nowDate = new Date();
        let key = nowDate.getFullYear().toString() + nowDate.getMonth().toString() + nowDate.getDate().toString();
        if(this.giftTime!=key){
            this.giftTime = key;
            this.saveUserInfo();
        }

    }
    



    //保存数据
    public saveUserInfo(): void {
		egret.localStorage.setItem("USERINFO", JSON.stringify(this));
	}
    static _ins: UserMgr;
	static get ins(): UserMgr {
		if (!this._ins) this._ins = new UserMgr();
		return this._ins;
	}


}